<?php


namespace app\controller\admin;


use app\common\Consts;
use app\model\admin\AdminUserModel;
use app\service\admin\AdminUserService;
use think\App;
use think\Request;

class AdminUserController extends BaseAdminController
{
    function setModel()
    {
        return new AdminUserModel();
    }

    function setWithModel()
    {
        return ['role'];
    }

    function setSearch()
    {
        $where = [];
        $where[] = ['id', '<>', 1];
        $userInfo = $this->request->middleware('userInfo');
        $roleId = $userInfo['role_id'];
        if ($roleId !== Consts::ADMIN_ROLE_ID) {
            $nextRoleId = get_child_role_id($roleId);
            $where[] = ['role_id', 'in', $nextRoleId];
        }
        return [
            'like' => ['username', 'nick'],
            '=' => ['role_id'],
            'custom' => $where
        ];
    }

    function setSort()
    {
        return [

        ];
    }

    /**
     * 用户信息
     * @return \think\response\Json
     */
    public function getUserInfo()
    {
        try {
            $userInfo = $this->request->middleware('userInfo');
            $data = [
                'id' => $userInfo['id'],
                'avatar' => $userInfo['avatar'],
                'nick' => $userInfo['nick'],
                'username' => $userInfo['username'],
                'role_id' => $userInfo['role_id'],
            ];
            return $this->success($data);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }

    public function loginOut()
    {
        try {
            $userInfo = $this->request->middleware('userInfo');
            $userInfo->valid_time = 0;
            $userInfo->save();
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }
}